package com.api.oa.mapper;

import com.api.oa.model.entity.YQTask;
import com.api.oa.model.vo.OaNoticeVo;
import com.api.oa.model.vo.YQTaskVO;
import com.api.oa.model.vo.YQTaskWeekVO;
import org.apache.ibatis.annotations.*;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
@Mapper
public interface YQTaskMapper {

    @Insert("insert into  yq_task  (" +
            "insCodeUser," +
            "patientUser," +
            "careUser," +
            "taskDate," +
            "taskContent," +
            "taskSat," +
            "taskStatus" +
            ") " +
            "values " +
            "(" +
            "#{entity.insCodeUser}," +
            "#{entity.patientUser}," +
            "#{entity.careUser}," +
            "#{entity.taskDate}," +
            "#{entity.taskContent}," +
            "#{entity.taskSat}," +
            "#{entity.taskStatus}" +
            ")")
    @Options(useGeneratedKeys = true, keyProperty = "taskId", keyColumn = "id")
    Integer insert(@Param("entity") YQTask entity);


    @Update("update  yq_task  set " +
            "insCodeUser = #{entity.insCodeUser}," +
            "patientUser = #{entity.patientUser}," +
            "careUser = #{entity.careUser}," +
            "taskDate = #{entity.taskDate}," +
            "taskSat = #{entity.taskSat}," +
            "taskStatus = #{entity.taskStatus}" +

            " where taskId = #{entity.taskId}")
    Integer update(@Param("entity") YQTask entity);


    @Delete("delete from yq_task where taskId=#{id}")
    void delete(@Param("id") Long id);


    @Select("<script> select * from  v_yq_task_user  <where> 1=1  " +
            "<if test='id!=null and id!=\"\"'>and insCodeUser = #{id} </if> " +
            "<if test='taskStatus!=null and taskStatus!=\"\"'> and taskStatus = #{taskStatus} </if> " +
            "<if test='startTime!=null and startTime != \"\"'> and taskDate between  #{startTime} and #{endTime}</if> " +
            "<if test='keyWord!=null and keyWord!=\"\"'> and concat(ifnull(patientName,''),ifnull(medName,''),ifnull(insName,'')) like concat('%', #{keyWord},'%') </if> </where></script> ")
    List<YQTaskVO> list(@Param("id") Long insCodeUser, @Param("taskStatus") String taskStats, @Param("keyWord") String keyWord, @Param("startTime") String start, @Param("endTime") String end);


    @Select("select yq_task.*, v_yq_care_user.medName as careUserName  from  yq_task left join v_yq_care_user on yq_task.careUser = v_yq_care_user.userId  where taskId = #{id} limit 1")
    YQTaskVO findOne(@Param("id") Long id);


    @Select("select *  from  v_yq_task_user  where taskId = #{id} limit 1")
    YQTaskVO findOneByID(@Param("id") Long id);


    //按照机构划分
    @Select("select * from  yq_task  where taskId = #{id}")
    List<YQTaskVO> listByIns(@Param("id") Long pid);


    @Select("<script>select taskDate,careUser,count(taskId) as patientCount from yq_task " +
            "where taskDate between #{start} and #{end} <if test='careUser!=null'>careUser = #{careUser} </if> GROUP BY  taskDate,careUser</script>")
    List<YQTaskWeekVO> pageByWeek(@Param("start") String start, @Param("end") String end, @Param("careUser") Long careUser);


    //按照医护
    @Select("<script>select * from yq_task left join v_yq_patient_user on yq_task.patientUser = v_yq_patient_user.userId" +
            "  where yq_task.careUser = #{id}  " +
            "<if test='isCare!=null'> and <if test='isCare'> taskStatus &gt; 1 </if> " +
            " <if test='!isCare'> taskStatus &lt; 2 </if> </if> </script>")
    List<YQTaskVO> listByCare(@Param("id") Long careUser, @Param("taskDate") String date, @Param("isCare") Boolean isCare);

    //详情
    @Select("select * from v_yq_task_user left join v_yq_patient_user on v_yq_task_user.patientUser = v_yq_patient_user.userId  where v_yq_task_user.taskId = #{taskId}")
    YQTaskVO findOneByTaskId(@Param("taskId") Long taskId);


    //更改状态
    @Update("update  yq_task  set " +
            "taskStatus = #{taskStatus}" +
            " where taskId = #{taskId}")
    Integer updateStatus(@Param("taskId") Long taskId, @Param("taskStatus") Integer taskStatus);


    @Select("select count(*) from yq_task where careUser=#{careUser} and patientUser=#{patientUser} and taskStatus = 0 ")
    Integer isExist(@Param("careUser") Long careUser, @Param("patientUser") Long patientUser);

    //按照医护
    @Select("<script>select * from v_yq_patient_user " +
            "  where userId = #{id} </script>")
    YQTaskVO findOneByUserId(@Param("id") Long careUser);

    //更改状态
    @Update("update  yq_task  set " +
            "taskLen = #{taskLen}," +
            "taskStatus = #{taskStatus}" +
            " where taskId = #{taskId}")
    Integer updateTimeById(@Param("taskId") Long taskId, @Param("taskStatus") Integer taskStatus, @Param("taskLen") Long time);


    @Select("<script>select * from v_yq_patient_user " +
            "  where userId = #{id} </script>")
    YQTask findOneByDateAndPatient(@Param("taskDate") String date, @Param("patientUserId") Long patient);


    //病患申请服务列表 web
    @Select("<script> select * from  v_yq_task_user  <where>  careUser is null " +
            "<if test='id!=null and id!=\"\"'>and insCodeUser = #{id} </if> " +
            "<if test='taskStatus!=null and taskStatus!=\"\"'> and taskStatus = #{taskStatus} </if> " +
            "<if test='startTime!=null and startTime != \"\"'> and taskDate between  #{startTime} and #{endTime}</if> " +
            "<if test='keyWord!=null and keyWord!=\"\"'> and concat(ifnull(patientName,''),ifnull(medName,''),ifnull(insName,'')) like concat('%', #{keyWord},'%') </if> </where></script> ")
    List<YQTaskVO> queryPatientReq(@Param("id") Long insCodeUser, @Param("taskStatus") String taskStats, @Param("keyWord") String keyWord, @Param("startTime") String start, @Param("endTime") String end);

    //Android端查询自己
    @Select("<script> select * from  v_yq_task_user  where patientUser = #{patientUser}  </script>")
    List<YQTaskVO> queryTaskByPatient(@Param("patientUser") Long userCode, @Param("isRange") Boolean isRange);


    @Update("update  yq_task  set " +
            "careUser = #{careUser}" +
            " where taskId = #{taskId}")
    Integer updateCareUserByTaskId(@Param("taskId") Long taskId, @Param("careUser") Long careUser);


    //患者提交满意度
    @Update("update  yq_task  set " +
            "taskSta = #{taskSta}" +
            " where taskId = #{taskId}")
    Integer updateStaByTaskId(@Param("taskId") Long taskId, @Param("taskSta") Integer taskSta);


    //Android端查询自己的护理记录
    @Select("<script> select * from  v_yq_task_user where patientUser = #{patientUser} and taskStatus &gt; 1 </script>")
    List<YQTaskVO> queryService(@Param("patientUser") Long patientUserId);


    @Select("<script> select * from  v_yq_task_user where careUser = #{careUser} and taskStatus &gt; 1 </script>")
    List<YQTaskVO> queryServiceByCare(@Param("careUser") Long careUser);


    //更改状态
    @Update("update  yq_task  set " +
            "taskSat = #{taskSat}" +
            " where taskId = #{taskId}")
    Integer updateStatisfcation(@Param("taskId") Long taskId, @Param("taskSat") String taskSat);


}

